package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrOctetString;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * SubChoiceAppUnit类
 * 实现应用单元选择的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class SubChoiceAppUnit implements AxdrType {

    private byte[] dataCode = null;
    private AppUnitChoice choice = AppUnitChoice._ERR_NONE_SELECTED;
    private AxdrOctetString appUnitPalin = null;
    private AxdrOctetString appUnit = null;
    private AxdrEnum dar = null;

    public enum AppUnitChoice {
        _ERR_NONE_SELECTED(-1),
        APP_UNIT_PALIN(0),
        APP_UNIT(1),
        DAR(2);

        private final int value;

        AppUnitChoice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static AppUnitChoice fromValue(int value) {
            for (AppUnitChoice choice : AppUnitChoice.values()) {
                if (choice.value == value) {
                    return choice;
                }
            }
            return _ERR_NONE_SELECTED;
        }
    }

    public SubChoiceAppUnit() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAppUnit(AxdrOctetString appUnit) {
        this.resetChoices();
        this.appUnit = appUnit;
        this.choice = AppUnitChoice.APP_UNIT;
    }

    public void setAppUnitPalin(AxdrOctetString appUnitPalin) {
        this.resetChoices();
        this.appUnitPalin = appUnitPalin;
        this.choice = AppUnitChoice.APP_UNIT_PALIN;
    }

    public void setDar(AxdrEnum dar) {
        this.resetChoices();
        this.dar = dar;
        this.choice = AppUnitChoice.DAR;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        com.dl698.asn1.axdr.AxdrEnum choosen = new com.dl698.asn1.axdr.AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = AppUnitChoice.fromValue((int) choosen.getValue());

        if (this.choice == AppUnitChoice.APP_UNIT_PALIN) {
            this.appUnitPalin = new AxdrOctetString();
            codeLength += this.appUnitPalin.decode(input);
            return codeLength;
        } else if (this.choice == AppUnitChoice.APP_UNIT) {
            this.appUnit = new AxdrOctetString();
            codeLength += this.appUnit.decode(input);
            return codeLength;
        } else if (this.choice == AppUnitChoice.DAR) {
            this.dar = new AxdrEnum();
            this.dar.setConst();
            codeLength += this.dar.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (java.io.IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == AppUnitChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == AppUnitChoice.DAR) {
            if (this.dar != null) {
                codeLength += this.dar.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("dar is null");
            }
        } else if (this.choice == AppUnitChoice.APP_UNIT) {
            if (this.appUnit != null) {
                codeLength += this.appUnit.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("appunit is null");
            }
        } else if (this.choice == AppUnitChoice.APP_UNIT_PALIN) {
            if (this.appUnitPalin != null) {
                codeLength += this.appUnitPalin.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(0);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("appunitpalin is null");
            }
        }
        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding SubChoiceAppUnit", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public AppUnitChoice getChoiceIndex() {
        return this.choice;
    }

    public AxdrOctetString getAppUnit() {
        return this.appUnit;
    }

    public AxdrOctetString getAppUnitPalin() {
        return this.appUnitPalin;
    }

    public AxdrEnum getDar() {
        return this.dar;
    }

    public void resetChoices() {
        this.choice = AppUnitChoice._ERR_NONE_SELECTED;
        this.appUnitPalin = null;
        this.appUnit = null;
        this.dar = null;
    }

    @Override
    public String toString() {
        if (this.choice == AppUnitChoice.APP_UNIT_PALIN) {
            if (this.appUnitPalin != null) {
                return "choice: {appUnitPalin: " + this.appUnitPalin + "}";
            } else {
                return "choice is appUnitPalin but appUnitPalin is null";
            }
        } else if (this.choice == AppUnitChoice.APP_UNIT) {
            if (this.appUnit != null) {
                return "choice: {appUnit: " + this.appUnit + "}";
            } else {
                return "choice is appUnit but appUnit is null";
            }
        } else if (this.choice == AppUnitChoice.DAR) {
            if (this.dar != null) {
                return "choice: {dar: " + this.dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        } else {
            return "unknown";
        }
    }
}